<?php

require('config.php');
$language_link=$_GET['id'];
$language_code = isset($_POST['language_code'])?$_POST['language_code']:language_default;

//$_POST['language_code']=$language_code;
$page = new rootLayoutPage();
$page->addJavaScript('jQuery.calendar.js');
$page->addStyleSheet('jquery-calendar.css');
$page->addJavaScript('keyEnable.js');
$page->addJavaScript('jQuery.AjaxSelect.js');
$page->addJavaScript('jQuery.AjaxForm.js');
$page->addJavaScript('jQuery.URLcontent.location.map.address.js');
//$page->setPageSameLevel($category_map->id_menu_left_tree);
//-- Menu Page ----------------------------------------------//
$page->setBaliseTitle('place_balise');
$page->setDescription('place_description');
if(isset($_POST['language_code'])){
	$xml = tmxTranslate('place-translate.xml', REP_XLAT_TEMPLATE,$_POST['language_code']);
	$xml_place = tmxTranslate('place-create.xml', REP_XLAT_TEMPLATE,$_POST['language_code']);
	$xml_subject = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL,$_POST['language_code']);
	$xml_city = tmxTranslate('url_city.xml', REP_XLAT_SCRIPT,$_POST['language_code']);
}else{
	$xml = tmxTranslate('place-translate.xml', REP_XLAT_TEMPLATE);
	$xml_place = tmxTranslate('place-create.xml', REP_XLAT_TEMPLATE);
	$xml_subject = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL);
	$xml_city = tmxTranslate('url_city.xml', REP_XLAT_SCRIPT);
}
$menu = new layoutMenuLeft();
	$menuPage = new layoutMenuPage();
	$menuPage->setTable('category_map');
	//$menuPage->setIdShow($category_map->id_menu_left_tree);
	//$menu->setIdShow($category_map->id_menu_left_tree);
	$menu->pushMenu($menuPage);

//--------Menu indicator--------------//
$menuIndictor = new layoutMenuIndicator();
	//$menuIndictor->idTree($category_map->id_menu_left_tree);
	//$menuIndictor->title($category_map->title);
	$page->setMenuIndicator($menuIndictor);

//************************//
	if(!$_SESSION['id_user']) redirect('connexion');
	if($_GET['id']!=null && $_GET['id']>0){
		if(onlyNumber($_GET['id'])){//Check User in Group Admin for this location//
			$location = modelLocationMap::getMyLocationList($_SESSION['id_user'],$_GET['id']);
			if(!$location){if(!modelUser::isUserGroupAdmin($_SESSION['id_user'])){redirectFront(LANGUAGE_FOLDER."/my-location");}}
		}else{redirectFront(LANGUAGE_FOLDER."/my-location");}
	}
//************************//

$location_map = modelLocationMap::getList($_GET['id']);
$getCategory=modelMap::getListCategoryTestSelect($_GET['id'],$language_code);
$getTypeReal=modelRealEstate::getLocationMapRealEstateSelect($_GET['id'],$language_code);
$city = modelCity::getListCitySelect($_GET['id'],$language_code);
$type=modelLocationMap::getListTypeAccessSelect($_GET['id'],$language_code);
$listKey = modelKeyWord::getKeywordProperty($_GET['id'],$language_code);

if(isset($_POST['create']) and $_POST['create']<>'') {
	 try {
	 	  $full_text = "";
		  $space = " ";
		  $name_place = addslashes(trim($_POST['place_name']));
		  $address = addslashes(trim($_POST['address']));
		  $id_province = addslashes(trim($_POST['province']));
		  $id_district = addslashes(trim($_POST['district']));		
		  $price_rental = $_POST['price_rental'];
		  $price_sell = floatval($_POST['price_sell']);
	      $rental_commission = $location_map->rental_commission;
	      $sale_price_owner = $location_map->sell_price_owner;
	      $start_date = $location_map->start_date;
	      $end_date = $location_map->end_date;
	      
		  $length = floatval($_POST['length']);
		  $width = floatval($_POST['width']);
		  $height = floatval($_POST['height']);
		
		  $size_building = floatval($_POST['size_building']);
		  $length_land = floatval($_POST['length_land']);
		  $width_land = floatval($_POST['width_land']);
		  $size_land = floatval($_POST['size_land']);
		  $bedroom = floatval($_POST['bedroom']);
		  $bathroom = floatval($_POST['bathroom']);
		
		
		  $mezzanine = $_POST['mezzanine'];
		  if($mezzanine=='No')$mezzanine=0;
		  else $mezzanine =1;
		  $floor = $_POST['floor'];
		  $number_floor = $_POST['number_floor'];
		  $tenant = $_POST['tenant'];
		  if($tenant=='No')$tenant=0;
		  else $tenant =1;
		  if($_POST['end_tenant']!=""){
		    $end_tenant = addslashes($_POST['end_tenant']);
		    $date_ajout = explode('/', $end_tenant);
		    $end_tenant = mktime(0, 0, 0, $date_ajout[1], $date_ajout[0], $date_ajout[2]);
		 }else{
			$end_tenant = 0;
		 }
		
		 $end_tenant = $tenant==1?$end_tenant:'';
		
		
		$furnished_apartment = isset($_POST['furnished_apartment'])=='yes'?1:0;
		$furnished_detail = $furnished_apartment==1?addslashes(trim($_POST['furnished_detail'])):'';
		
		  $latitude = $location_map->latitude;
		  $longitude = $location_map->longitude;
		  $content = addslashes(trim($_POST['content']));
		  $language_code = $_POST['language_code'];//echo "language_code=".$language_code;exit();
		  $index = isset($_GET['id'])?(isset($_POST['index'])?1:0):1;
		  $follow = isset($_GET['id'])?(isset($_POST['follow'])?1:0):1;
				
		  $url_rewriting = addslashes($_POST['url_rewriting']);
		  $meta_title = addslashes($_POST['meta_title']);
		  $meta_description = addslashes($_POST['meta_description']);
		
			$full_text .= $name_place.$space;
					  $full_text .= $address.$space;
					  $full_text .= $zip_code.$space;
					  $full_text .= $furnished_detail.$space;
					  $full_text .= $content.$space;
					  $full_text .= addslashes(trim($city->title_province)).$space;
		
					$mezzanine_text = $xml_place['label_1_12'];
					$furnished_apartment_text = $xml_place['label_1_13'];
					$tenant_text = $xml_place['label_1_17'];
					$full_text .= $mezzanine ==1?addslashes(trim($mezzanine_text)).$space:'';
					$full_text .= $furnished_apartment ==1?addslashes(trim($furnished_apartment_text)).$space:'';
					$full_text .= $tenant ==1?addslashes(trim($tenant_text)).$space:'';
			
		$user = modelUser::getProfil($_SESSION['id_user']);
		$count = modelLocationMap::countMyLocationVisible();
		if($user->total_credit > $count->count_credit){
			$visible = 1;
		}else{
			$visible = 0;
		}
		
		
		foreach($getCategory as $key => $category) {
			$full_text .= addslashes(trim($category->title)).$space;
		}
	
		foreach($listKey as $key => $k){
			$full_text .= addslashes(trim($keyword->title)).$space;
		}
	
		//*** Create map ********************************************//
			if(!modelLocationMap::getExistPlace($language_link)){
				$id = modelLocationMap::insertAddressMarker($name_place,$address,$language_link,$language_code,$visible,$content,$furnished_detail,$url_rewriting,$meta_title,$meta_description,1,$full_text);
				$id_shared = modelLocationMap::insertPlaceShared($id_province,$id_district,$latitude,$longitude,$length,$width,$height,$length_land,$width_land,$size_building,$size_land,$bedroom,$bathroom,$mezzanine,$furnished_apartment,$floor,$number_floor,$tenant,$end_tenant,$start_date,$end_date,$follow,$index);
				$id_price = modelLocationMap::insertPlacePrice($language_link,$price_rental,$price_sell,$rental_commission,$sale_price_owner);			
				modelLocationMap::updatePriceLanguageLink($id_price);
				modelLocationMap::updateSharedPriceLink($id,$language_link,$id_shared,$id_price);
			}else{
				$id = modelLocationMap::insertAddressMarker($name_place,$address,$language_link,$language_code,$visible,$content,$furnished_detail,$url_rewriting,$meta_title,$meta_description,1,$full_text);
				$getPlace = modelLocationMap::getPlaceById($language_link);
				$id_map_address_shared = $getPlace->id_map_address_shared;
				$id_map_address_price = $getPlace->id_map_address_price;
				modelLocationMap::updateSharedPriceLink($id,$language_link,$id_map_address_shared,$id_map_address_price);
			}
			//***********************************************************//
		Persistence::register(PERSIST_MESSAGE, new Message($xml['message_1'], Message::INFO));

		$html_name = $location_map->url_rewriting.'-'.$location_map->language_link.'.html';
		redirectFrontHtml($html_name);
	}
    catch(Exception $e) {
    	echo $e ;
        Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message::AVERT));
    }
}
if(!isset($_POST['language_code'])){
	$html_name = $location_map->url_rewriting.'-'.$location_map->language_link.'.html';
	redirectFrontHtml($html_name);
}
$tpl_file = 'place-translate.tpl';
$place = new flyLayout(REP_TPL . $tpl_file);
$place->start();
		$place->setVariable('place_name', htmlspecialchars($location_map->name_place));
		if($location_map->address!=''){
			$place->setVariable('address', htmlspecialchars($location_map->address));
			$place->showBlock('address');
		}
		if($location_map->content!=''){
			$place->setVariable('content', htmlspecialchars($location_map->content));
			$place->showBlock('content');
		}
		
		if($location_map->price_rental<>0){
			$place->setVariable('price_rental',  number_format($location_map->price_rental,2,'.',' ').' '.CURRENCY);
			$place->showBlock('price_rental');
		}
		if($location_map->length<>0){
			$place->setVariable('length', $location_map->length);
			$place->showBlock('length');
		}
		if($location_map->width<>0){
			$place->setVariable('width', $location_map->width);
			$place->showBlock('width');
		}

		if($location_map->height<>0){
			$place->setVariable('height', $location_map->height);
			$place->showBlock('height');
		}
		if($location_map->mezzanine==1){
			$place->setVariable('checked_mezzanine', $location_map->mezzanine?'Yes':'No');
			$place->showBlock('checked_mezzanine');
		}
		if($location_map->furnished_apartment==1){
			$place->setVariable('checked_furnished_apartment', $location_map->furnished_apartment?'Yes':'No');
			$place->showBlock('checked_furnished_apartment');
		}
		if($location_map->furnished_detail!=''){
			$place->setVariable('furnished_detail', $location_map->furnished_detail);
			$place->showBlock('furnished_detail');
		}
		if($location_map->floor<>0){
			$place->setVariable('floor', $location_map->floor);
			$place->showBlock('floor');
		}
		if($location_map->number_floor<>0){
			$place->setVariable('number_floor', $location_map->number_floor);
			$place->showBlock('number_floor');
		}
		if($location_map->tenant==1){
			$place->setVariable('checked_tenant', $location_map->tenant?'Yes':'No');
			$place->showBlock('checked_tenant');
		}
		$end_tenant = $location_map->end_tenant!=0?date("d/m/Y",$location_map->end_tenant):'';
		if($location_map->end_tenant>1){
			$place->setVariable('end_tenant', $end_tenant?$end_tenant:'');
			$place->showBlock('end_tenant');
		}
		if($location_map->price_sell<>0){
			$place->setVariable('price_sell',number_format($location_map->price_sell,2,'.',' ').' '.CURRENCY);
			$place->showBlock('price_sell');
		}
		if($location_map->size_building<>0){
			$place->setVariable('size_building', $location_map->size_building);
			$place->showBlock('size_building');
		}
		if($location_map->length_land<>0){
			$place->setVariable('length_land', $location_map->length_land);
			$place->showBlock('length_land');
		}
		if($location_map->width_land<>0){
			$place->setVariable('width_land', $location_map->width_land);
			$place->showBlock('width_land');
		}
		if($location_map->size_land<>0){
			$place->setVariable('size_land', $location_map->size_land);
			$place->showBlock('size_land');
		}
		if($location_map->bedrooms<>0){
			$place->setVariable('bedroom', $location_map->bedrooms);
			$place->showBlock('bedroom');
		}
		if($location_map->bathrooms<>0){
			$place->setVariable('bathroom', $location_map->bathrooms);
			$place->showBlock('bathroom');
		}
		if($location_map->reference<>""){
			$place->setVariable('reference', $location_map->reference);
			$place->showBlock('block_reference');
		}

		$place->setVariable('checked_index', $location_map->index?'checked="checked"':'');
		$place->setVariable('checked_follow', $location_map->follow?'checked="checked"':'');
		$place->setVariable('url_rewriting', htmlspecialchars($location_map->url_rewriting));
		$place->setVariable('meta_title', htmlspecialchars($location_map->meta_title));
		$place->setVariable('meta_description', htmlspecialchars($location_map->meta_description));
		$place->setVariable('id_language_link', $_GET['id']);
		$place->setVariable('language_code', $_POST['language_code']);

		$place->setVariable('id__province',$city->id_province);
		$place->setVariable('id__district',$city->id_district);
		$place->setVariable('name_province',$city->title_province?$city->title_province:'(aucun)');

	//======================
	$title_estate = $getTypeReal->title ;
	$estate_id = $getTypeReal->id_real_estate_type ;
	$place->setVariable('real_title',$title_estate);
	$place->setVariable('real_id',$estate_id);


	foreach($getCategory as $key => $category) {
		$place->setVariable('category_title',$category->title);
		$place->setVariable('id_category_map',$category->id_category_map);
		//$place->setVariable('checked',$category->link==$category->language_link?'CHECKED="CHECKED"':'');
		$place->parseList('list_category');
	}

	if($listKey->size()>0){
		$seper=0;
		if($listKey->size()>5) {
			for($i=1;$i<$listKey->size()/2;$i++){
				$all = $listKey->size();
				if($all-($i+$i)<=$i){ $seper = $i; break;}

			}
		}
		if($listKey->size()>0) {
			foreach($listKey as $index => $keyword) {
				//$indexOrder = $index + 1;
				if($seper==0){
					$place->setVariable('title1', $keyword->title);
					$place->setVariable('key_link_1', $keyword->language_link);
					$place->parseList('listkeyword_1');
				}else{
					if($index<$seper){
						$place->setVariable('title1', $keyword->title);
						$place->setVariable('key_link_1', $keyword->language_link);
						$place->parseList('listkeyword_1');
					}else if($index>=$seper and $index<$seper+$seper){
						$place->setVariable('title2', $keyword->title);
						$place->setVariable('key_link_2', $keyword->language_link);
						$place->parseList('listkeyword_2');
					}else if($index>=$seper+$seper){
						$place->setVariable('title3', $keyword->title);
						$place->setVariable('key_link_3', $keyword->language_link);
						$place->parseList('listkeyword_3');
					}
				}
			}
		}
		$place->showBlock('key');
	}
	//$place->setVariable('rep_ajax', REP_AJAX);

if(isset($_SESSION['language'])==null && $_POST['language_code']=='' &&!isTranslating()){
	$html_name = $location_map->url_rewriting.'-'.$location_map->language_link.'.html';
	redirectFrontHtml($html_name);
}
$place->setVariable('popup_1_1',isset($xml_place['popup_1_1'])?$xml_place['popup_1_1']:'');
$place->setVariable('popup_1_2',isset($xml_place['popup_1_2'])?$xml_place['popup_1_2']:'');
$place->setVariable('popup_1_3',isset($xml_place['popup_1_3'])?$xml_place['popup_1_3']:'');
$place->setVariable('popup_1_15',isset($xml_place['popup_1_15'])?$xml_place['popup_1_15']:'');//$xml_place['popup_1_15']);

$place->setVariable('label_1_1',$xml_place['label_1_1']);
$place->setVariable('label_1_2',$xml_place['label_1_2']);
$place->setVariable('label_1_4',$xml_place['label_1_4']);
$place->setVariable('xml_reference',$xml_place['xml_reference']);
//$place->setVariable('label_1_7',$xml_place['label_1_7']);
$place->setVariable('label_1_8',$xml_place['label_1_8']);
$place->setVariable('label_1_9',$xml_place['label_1_9']);
$place->setVariable('label_1_10',$xml_place['label_1_10']);

$place->setVariable('label_1_11',$xml_place['label_1_11']);
$place->setVariable('label_1_12',$xml_place['label_1_12']);
$place->setVariable('label_1_13',$xml_place['label_1_13']);
$place->setVariable('label_1_14',$xml_place['label_1_14']);
$place->setVariable('label_1_15',$xml_place['label_1_15']);
$place->setVariable('label_1_16',$xml_place['label_1_16']);
$place->setVariable('label_1_17',$xml_place['label_1_17']);
$place->setVariable('label_1_18',$xml_place['label_1_18']);
$place->setVariable('label_1_19',$xml_place['label_1_19']);
$place->setVariable('label_1_20',$xml_place['label_1_20']);

$place->setVariable('label_1_21',$xml_place['label_1_21']);
$place->setVariable('label_1_23',$xml_place['label_1_23']);
$place->setVariable('label_1_25',$xml_place['label_1_25']);
$place->setVariable('label_1_26',$xml_place['label_1_26']);
$place->setVariable('label_1_27',$xml_place['label_1_27']);
$place->setVariable('label_1_28',$xml_place['label_1_28']);
$place->setVariable('label_1_29',$xml_place['label_1_29']);
$place->setVariable('label_1_30',$xml_place['label_1_30']);
$place->setVariable('label_1_31',$xml_place['label_1_31']);
$place->setVariable('label_1_32',$xml_place['label_1_32']);
$place->setVariable('button_1',$xml_place['button_2']);

$place->stop();
$page->setBody($place);
$page->display();

?>